<?php

class ml_biz_search_user {
    
    const BLOCK_SIZE = 20;
    
    private $url;
    private $aid;
    private $blockSum = 0;
    private $sum;
    
    private $num = self::BLOCK_SIZE;
    private $start = 0;
    private $searchKey;
    private $searchWhere;
    
    
    const SEARCH_USER_URL = "http://220.181.136.237/meila_user/srm.php?qjson=";
    
    public function getSearchResult() {
        
        if (empty($this->searchKey)) return array();
        
        $this->initUrl();
        $rs = Tool_http::get($this->url);
        $rs = json_decode($rs, true);
        
        $this->sum = $rs['sp']['m2'];
        if (empty($this->sum)) return array();
        
        $this->blockSum = intval($this->sum/$this->num);
        
        $aUid = array();
        foreach ($rs['sp']['result'] as $row){
            $aUid[] = $row['userid'];
            $row['stags'] = strip_tags($row['stags']);
            $tagList[$row['userid']] = explode(',', $row['stags']);
        }
        
        $oBizPin = new ml_biz_userListOutput($aUid, $this->aid, $tagList);
        return $oBizPin->render_html_list();
    }
    
    public function __construct($keyword, $aid, $block = 1){
        $this->setKeyword($keyword);
        $this->aid = $aid;
        $this->setSearchBlock(intval($block));
        $this->setSearchWhere();
    }
    
    private function setKeyword($keyword) {
        
        $keyword = trim($keyword);
        $keyword = mb_convert_encoding($keyword, 'GBK' , 'utf-8' );
        $this->searchKey = urlencode($keyword);
    }
    
    public function get_total_block(){
        return intval($this->blockSum);
    }
    
    public function get_total_number(){
        return intval($this->sum);
    }
    
    public function setSearchWhere($where) {
        if ($where === 'tag') {
            $this->searchWhere = 'stags:';
        } else {
            $this->searchWhere = 'nickname:';
        }
    }
    
    public function setSearchPrice() {
        
    }
    
    private function initUrl(){
        $this->url = self::SEARCH_USER_URL. $this->searchWhere. $this->searchKey. '+x_where:status<10+x_sort:popular&num='. $this->num. '&start='. $this->start;
    }
    
    public function setSearchStart($start) {
        $this->start = intval($start);
    }
    
    public function setSearchNum($num) {
        $this->num = intval($num);
    }
    
    public function setSearchBlock($block){
        
        $this->start = self::BLOCK_SIZE * intval($block-1);
        
    }
    
    
}